CMAKE_FLAGS					+= -DCMAKE_BUILD_TYPE=Debug

MACHINE_ESP32				= esp32
MACHINE_ESP32_DEVBOARD		= esp32_devboard
MACHINE_X86_64				= x86_64
MACHINE_ARM					= arm
MACHINE_ARM64				= arm64

ARM_GCC_PRENAME				= arm-linux-gnueabihf
ARM64_GCC_PRENAME			= aarch64-linux-gnu

NINJA_BUILD					:= $(shell ninja --version 2> /dev/null)

ifdef NINJA_BUILD
	CMAKE_GENERATOR ?= "Ninja"
	MAKER = ninja
	SILENT =
else
	CMAKE_GENERATOR ?= "Unix Makefiles"
	MAKER = make
	SILENT = --silent
endif

all: $(MACHINE_X86_64)

### X86_64 BEGIN ########################################################################################
mkbuild_$(MACHINE_X86_64):
	@if [ ! -e $(PWD)/build/$(MACHINE_X86_64)/CMakeCache.txt ]; \
	then \
		mkdir -p $(PWD)/build/$(MACHINE_X86_64)/; \
	fi

$(MACHINE_X86_64): mkbuild_$(MACHINE_X86_64)
	@cp platforms/linux/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_X86_64)
	@cd build/$(MACHINE_X86_64) && cmake -DCC4TARGET=$(MACHINE_X86_64) ../../ -G$(CMAKE_GENERATOR) $(CMAKE_FLAGS) && $(MAKER) ${SILENT}
### X86_64 END ##########################################################################################


### ARM BEGIN ###########################################################################################
mkbuild_$(MACHINE_ARM):
	@if [ ! -e $(PWD)/build/$(MACHINE_ARM)/CMakeCache.txt ]; \
	then \
		mkdir -p $(PWD)/build/$(MACHINE_ARM)/; \
	fi

$(MACHINE_ARM): mkbuild_$(MACHINE_ARM)
	@cp platforms/linux/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ARM)
	@cd build/$(MACHINE_ARM) && cmake -DCC4TARGET=$(MACHINE_ARM) -DCMAKE_TOOLCHAIN_FILE=../../tools/toolchains/Toolchain-arm-linux-gnueabihf.cmake ../../ -G$(CMAKE_GENERATOR) $(CMAKE_FLAGS) && $(MAKER) ${SILENT}
### ARM END #############################################################################################

### ARM64 BEGIN #########################################################################################
mkbuild_$(MACHINE_ARM64):
	@if [ ! -e $(PWD)/build/$(MACHINE_ARM64)/CMakeCache.txt ]; \
	then \
		mkdir -p $(PWD)/build/$(MACHINE_ARM64)/; \
	fi

$(MACHINE_ARM64): mkbuild_$(MACHINE_ARM64)
	@cp platforms/linux/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ARM64)
	@cd build/$(MACHINE_ARM64) && cmake -DCC4TARGET=$(MACHINE_ARM64) -DCMAKE_TOOLCHAIN_FILE=../../tools/toolchains/Toolchain-aarch64-linux-gnu.cmake ../../ -G$(CMAKE_GENERATOR) $(CMAKE_FLAGS) && $(MAKER) ${SILENT}
### ARM64 END ###########################################################################################

### ESP32 BEGIN #########################################################################################
mkbuild_$(MACHINE_ESP32):
	@if [ ! -e $(PWD)/build/$(MACHINE_ESP32)/CMakeCache.txt ]; \
	then \
		mkdir -p $(PWD)/build/$(MACHINE_ESP32)/; \
	fi
	
$(MACHINE_ESP32)_target: mkbuild_$(MACHINE_ESP32)
	@cp platforms/$(MACHINE_ESP32)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32) set-target esp32s3

$(MACHINE_ESP32): mkbuild_$(MACHINE_ESP32)
	@cp platforms/$(MACHINE_ESP32)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32) build

$(MACHINE_ESP32)_menuconfig: mkbuild_$(MACHINE_ESP32)
	@cp platforms/$(MACHINE_ESP32)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32)/CMakeLists.txt ./
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32) menuconfig
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32)

$(MACHINE_ESP32)_flash: mkbuild_$(MACHINE_ESP32)
	@cp platforms/$(MACHINE_ESP32)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32) build flash

$(MACHINE_ESP32)_flash_monitor: mkbuild_$(MACHINE_ESP32)
	@cp platforms/$(MACHINE_ESP32)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32) build flash monitor
### ESP32 END ###########################################################################################


### ESP32_DEVBOARD BEGIN ################################################################################
mkbuild_$(MACHINE_ESP32_DEVBOARD):
	@if [ ! -e $(PWD)/build/$(MACHINE_ESP32_DEVBOARD)/CMakeCache.txt ]; \
	then \
		mkdir -p $(PWD)/build/$(MACHINE_ESP32_DEVBOARD)/; \
	fi

$(MACHINE_ESP32_DEVBOARD)_target: mkbuild_$(MACHINE_ESP32_DEVBOARD)
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/CMakeLists.txt ./
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32_DEVBOARD) set-target esp32s3
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32_DEVBOARD)

$(MACHINE_ESP32_DEVBOARD): mkbuild_$(MACHINE_ESP32_DEVBOARD)
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32_DEVBOARD)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32_DEVBOARD) build

$(MACHINE_ESP32_DEVBOARD)_menuconfig: mkbuild_$(MACHINE_ESP32_DEVBOARD)
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32_DEVBOARD)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32_DEVBOARD) menuconfig

$(MACHINE_ESP32_DEVBOARD)_flash: mkbuild_$(MACHINE_ESP32_DEVBOARD)
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32_DEVBOARD)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32_DEVBOARD) build flash

$(MACHINE_ESP32_DEVBOARD)_flash_monitor: mkbuild_$(MACHINE_ESP32_DEVBOARD)
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/sdkconfig ./
	@cp platforms/$(MACHINE_ESP32_DEVBOARD)/CMakeLists.txt ./
	@python ./tools/python/PyMaker.py $(MACHINE_ESP32_DEVBOARD)
	idf.py --build-dir $(PWD)/build/$(MACHINE_ESP32_DEVBOARD) build flash monitor
### ESP32_DEVBOARD END ####################################################################################


clean:
	@rm -rf build/
